<?php
	/**
	 * socket基类
	 * detail description
	 * @todo
	 * @author swingcoder <swingcoder@msn.com>
	 * @version 0.1
	 * @copyright Copyright (c) 2009, swingcoder.com
	 */

	interface socket_schema
	{
		function set_command($data='');
		function send();
		function __send();
		function close();
		function check();
		function result($result);
		function parse_result($result);
	}

	abstract class socket implements socket_schema
	{
		protected $fp = null;
		private $host = '';
		private $port = 80;
		private $timeout = 30;
		public $cmd = array();
		private $errmsg = '';
		public $debug = false;
		public $result = '';

		function socket( $host, $port=80, $timeout=30)
		{
			set_error_handler( array( $this, 'error' ) );
			error_reporting(E_ALL ^ E_NOTICE);
			//set_magic_quotes_runtime( 0 );//deprecated in php 3.x
			set_time_limit( 60 );

			$this->host = $host;
			$this->port = $port;
			$this->timeout = $port;
		}

		function set_command($cmd='')
		{
			$this->cmd[] = $cmd;
		}

		function send()
		{
			if( $this->check() )
			{
				$result = $this->__send();
			}
			else
			{
				trigger_error($this->errmsg, 256);
			}

			if( ! $result )
			{
				trigger_error($this->errmsg, 256);
			}
			else
			{
				$this->result = $this->result($result);
			}
		}

		function __send()
		{
			$this->fp = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
			if( ! $this->fp )
			{
				$this->errmsg = 'Create socket failure: ' . $errno . '->' . $errstr;
				return false;
			}
			$cmd = implode("",$this->cmd);
			if( fwrite($this->fp, $cmd) === FALSE )
			{
				$this->errmsg = 'Write socket failure.';
				return false;
			}

			$result = "";
			while( ! feof($this->fp) )
			{
				$result .= fgets($this->fp, 128);
			}
			if( empty($result) )
			{
				$this->errmsg = 'return null.';
				return false;
			}

			return $result;
		}

		function check()
		{
			if( empty($this->host) )
			{
				$this->errmsg = 'Host not defined.';
				return false;
			}
			else
			{
				return true;
			}
		}

		function error($no, $str, $file, $line)
		{
			$errmsg = "[" . date('Y-m-d H:i:s') . "]" . $str . " file " . $file . " on line " . $line . "\r\n\r\n";
			error_log( $errmsg, 3, '../log/phperror.log' );
			return false;
		}

		function result( $result)
		{
			if( $this->debug )
			{
				$debug_info = '';
				$debug_info .= "================Request================\r\n";
				$debug_info .= implode( "\r\n", $this->cmd);
				$debug_info .= "================Response================\r\n";
				$debug_info .= substr($result, 0, strpos($result, "\r\n\r\n") );
				$data = substr($result, strpos($result, "\r\n\r\n")+4 );
				$debug_info .= "\r\n\r\n================Data Length================\r\n";
				$debug_info .= strlen($data);

				$this->error( 0, $debug_info, __FILE__, __LINE__ );
			}
			return $this->parse_result($result);
		}

		function parse_result( $result )
		{
			return $result;
		}

		function close()
		{
		}
	}
?>